package gcontent.config.node.filter;

import gcontent.config.error.ConfigEx;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class FilterNode {
	
	private String id = "";
	private String name = "";
	private String input = "";
	
	public String getId()
	{
		return this.id;
	}
	
	public String getName()
	{		
		return this.name;
	}
	
	public String getInput()
	{
		return this.input;
	}
	
	public FilterNode(Node node) throws ConfigEx
	{
		NodeList node_list;			
		NamedNodeMap node_map;
		
		node_map = node.getAttributes();
		if(node_map == null)
			throw new ConfigEx("FilterNode : not id attribute defined");
		
		if(node_map.getNamedItem("id") == null)
			throw new ConfigEx("FilterNode : not id attribute defined");

		this.id = node_map.getNamedItem("id").getNodeValue();
		
		node_list = node.getChildNodes();
		for(int i = 0; node_list.item(i) != null; i++) {
			Node node_c;
			String name_n;
			
			node_c = node_list.item(i);
			name_n = node_c.getNodeName();

			if(name_n.equals("name")) {
				this.name = node_c.getTextContent();
			} else if(name_n.equals("input")) {
				this.input = node_c.getTextContent();
				for(int n = 0; n < this.input.length(); n++) {
					if(this.input.charAt(n) == '<') {
						this.input = this.input.substring(n, this.input.length());
						break;
					}
				}
			} else if(name_n.charAt(0) != '#')
				throw new ConfigEx("FilterNode => unknown node " + name);
		}
	}

}
